﻿// -----------------------------------------------------------------------
// <copyright file="ProjectReader.cs" company="Raop">
// Project reader.
// </copyright>
// -----------------------------------------------------------------------

namespace Raop.Library.Tools
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using System.Diagnostics.Contracts;
    using System.IO;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class ProjectReader
    {
        private const string nameSpace = "http://schemas.microsoft.com/developer/msbuild/2003";

        public string DirectoryName { get; private set; }

        public string ProjectFile { get; private set; }

        public ProjectReader(string projectFile)
        {
            Contract.Requires(projectFile != null);
            if (!File.Exists(projectFile))
            {
                throw new FileNotFoundException();
            }

            this.ProjectFile = projectFile;

            this.DirectoryName = new FileInfo(this.ProjectFile).DirectoryName;
        }

        public List<string> GetAssemblies()
        {
            Contract.Requires(this.ProjectFile != null);
            Contract.Ensures(Contract.Result<List<string>>() != null);

            XDocument document = XDocument.Load(this.ProjectFile);

            var files = from f in document.Element(XName.Get("Project", nameSpace))
                            .Elements(XName.Get("ItemGroup", nameSpace))
                            .Elements(XName.Get("Reference", nameSpace))
                        select f.Attribute("Include").Value;

            return files.ToList();
        }

        public List<string> GetFiles()
        {
            Contract.Requires(this.ProjectFile != null);
            Contract.Ensures(Contract.Result<List<string>>() != null);

            XDocument document = XDocument.Load(this.ProjectFile);

            var files = from f in document.Element(XName.Get("Project", nameSpace))
                            .Elements(XName.Get("ItemGroup", nameSpace))
                            .Elements(XName.Get("Compile", nameSpace))
                        select Path.Combine(this.DirectoryName, f.Attribute("Include").Value);

            return files.ToList();
        }

        public List<string> GetDependencyProject()
        {
            Contract.Requires(this.ProjectFile != null);
            Contract.Ensures(Contract.Result<List<string>>() != null);

            XDocument document = XDocument.Load(this.ProjectFile);

            var files = from f in document.Element(XName.Get("Project", nameSpace))
                            .Elements(XName.Get("ItemGroup", nameSpace))
                            .Elements(XName.Get("ProjectReference", nameSpace))
                        select Path.Combine(this.DirectoryName, f.Attribute("Include").Value);

            return files.ToList();
        }
    }
}
